function addedWord( ) {
  var sel = window.getSelection( ) ;
  var range = sel.getRangeAt( 0 ) ;
  var span = document.createElement( 'span' ) ;
  span.style.backgroundColor = "#ffff00" ;
  range.surroundContents( span ) ;
  // this logic might be not good.
  if( window.getSelection( ).toString( ) == '' ) {
    sel.removeAllRanges( ) ;
    sel.addRange( range ) ;
  }
}

function click( ) {

  var text = document.getSelection( ).toString( ) ;

  if( ! text )
    return ;

  text = text.replace( /^\s*/, '' ) ;
  text = text.replace( /\s*$/, '' ) ;

  if( text == '' )
    return ;

  chrome.extension.sendRequest( { 'action' : 'addWord',
                                  'word'   : text,
                                  'note'   : '',
                                  'uri'    : String( location.href ) },
                                  addedWord ) ;

}

document.addEventListener( 'mouseup', click, false ) ;

